package com.bsh.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bsh.api.Exception.ApiException;
import com.bsh.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.IpUtil;

/**
 * 交易相关API
 * 
 * @author jane
 * @since 0.0.1
 * 
 */
@Controller
@RequestMapping(value = "/api/v1/finance", method = { RequestMethod.POST })
public class ApiFinanceController {
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	ApiFinanceService apiFinanceService;

	/**
	 * 余额转账
	 * 
	 * @param paramStr
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/transfer/balance")
	@ResponseBody
	public ApiMessage transferByBalance(@RequestParam(value = "paramStr", required = true) String paramStr){
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiFinanceService.transferByBalance(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}

	/**
	 * 充值
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/user/recharge")
	@ResponseBody
	public ApiMessage userRecharge(@RequestParam(value = "paramStr", required = true) String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            JSONObject dto = (JSONObject) JSON.parse(paramStr);
            dto.put("ip", IpUtil.getIpAddr(request));
            msg = apiFinanceService.userRecharge(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage("400", "请求失败");
            e.printStackTrace();
        } finally {
            return msg;
        }
	}

	/**
	 * 在线支付转账
	 * 
	 * @param paramStr
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/transfer/online", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage transferByOnline(@RequestParam(value = "paramStr", required = true) String paramStr){
        ApiMessage msg = new ApiMessage();
        try {
            JSONObject dto = (JSONObject) JSON.parse(paramStr);
            dto.put("ip", IpUtil.getIpAddr(request));
            msg = apiFinanceService.transferByOnline(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage("400", "请求失败");
            e.printStackTrace();
        } finally {
            return msg;
        }
	}

	/**
	 * 提现申请
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/transfer/withdrawl", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage withdrawl(@RequestParam(value = "paramStr", required = true) String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            JSONObject dto = (JSONObject) JSON.parse(paramStr);
            dto.put("ip", IpUtil.getIpAddr(request));
            msg = apiFinanceService.withdrawl(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage("400", "请求失败");
            e.printStackTrace();
        } finally {
            return msg;
        }

	}

	/**
	 * 提现申请列表
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/transfer/withdrawlList", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage withdrawlList(@RequestParam(value = "paramStr", required = true) String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            JSONObject dto = (JSONObject) JSON.parse(paramStr);
            dto.put("ip", IpUtil.getIpAddr(request));
            msg = apiFinanceService.withdrawlList(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage("400", "请求失败");
            e.printStackTrace();
        } finally {
            return msg;
        }

	}

}
